import {localSet, localGet, localRemove, localRemoveAll} from '@/common/utils/GlobalUtils'
const initDefaultState = () => {
    localRemove('AccessToken')
    localRemove('RefreshToken')
    localRemove('UserName')
    localRemove('UserId')
    localRemove('UserAccount')
    localRemove('Email')
    localRemove('PhoneNumber')
    localRemove('DepartmentName')
    localRemove('DepartmentId')
    return {
        accessToken: '',
        refreshToken: '',
        username: '',
        userId: '',
        account: '',
        email: '',
        phoneNumber: '',
        departmentName: '',
        departmentId: '',
    }
}
// 用户信息全局变量
const state = {
    accessToken: '' || localGet('AccessToken'),
    refreshToken: '' || localGet('RefreshToken'),
    username: '' || localGet('UserName'),
    userId: '' || localGet('UserId'),
    account: '' || localGet('UserAccount'),
    email: '' || localGet('Email'),
    phoneNumber: '' || localGet('PhoneNumber'),
    departmentName: '' || localGet('DepartmentName'),
    departmentId:'' || localGet('DepartmentId'),
}
const mutations = {
    SET_TOKEN: (state, token) => {
        state.accessToken = token.access_token
        localSet('AccessToken', token.access_token)
        state.refreshToken = token.refresh_token
        localSet('RefreshToken', token.refresh_token)
    },
    SET_USERNAME: (state, username) => {
        state.username = username
        localSet('UserName', username)
      },
    SET_USERID: (state, userId) => {
        state.userId = userId
        localSet('UserId', userId)
    },
    SET_USERACCOUNT: (state, account) => {
        state.account = account
        localSet('UserAccount', account)
    },
    SET_EMAIL: (state, email) => {
        state.email = email
        localSet('Email', email)
    },
    SET_PHNOE: (state, phoneNumber) => {
        state.phoneNumber = phoneNumber
        localSet('PhoneNumber', phoneNumber)
    },
    SET_DEPARTMENT: (state, department) => {
        state.departmentName = department
        localSet('DepartmentName', department)
    },
    SET_DEPARTMENT_ID: (state, departmentId) => {
        state.departmentId = departmentId
        localSet('DepartmentId', departmentId)
    },
    RESET_TOKEN: state => {
        localRemove('AccessToken')
        state.accessToken = ''
    },
    RESET_REFRESHTOKEN: state => {
        localRemove('RefreshToken')
        state.refreshToken = ''
    },
    // 登出 及 RefreshToken失效时调用
    RESET_STATE: state => {
        const stateInfoObj = initDefaultState()
        Object.keys(stateInfoObj).forEach((key) => {
          state[key] = stateInfoObj[key]
        })
    },
    user_clear:(state)=>{
        for (let key in state){
            state[key] = '';
        }
    }
}
const actions = {
    // 调用登录。。。。。。
}

export default {
    state,
    mutations
}